package com.yx.backstage.controller;


import com.yx.backstage.manager.FileTypeManager;
import com.yx.backstage.model.param.FileTypeParam;
import com.yx.backstage.model.vo.FileTypeVo;
import com.yx.backstage.config.global.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 文件类型管理表 前端控制器
 * </p>
 *
 * @author YangXiong
 * @since 2023-03-25
 */
@Slf4j
@RestController
@RequestMapping("/file-type")
@Api(tags = "文件类型信息接口")
public class FileTypeController {
    @Resource
    private FileTypeManager fileTypeManager;

    @PostMapping("/type/page")
    @ApiOperation("文件类型列表")
    public CommonResult<List<FileTypeVo>> getPage(){
        return fileTypeManager.getPage();
    }

    @PostMapping("/type/list")
    @ApiOperation("根据种类获取列表")
    public CommonResult<List<FileTypeVo>> getPageByKind(@RequestBody FileTypeParam param){
        log.info("FileTypeController info getPageByKind FileTypeParam={}", param);
        return fileTypeManager.getPageByKind(param);
    }

    @PostMapping("/saveorupdate")
    @ApiOperation("新增或者更新类型")
    public CommonResult<Boolean> saveOrUpdate(@RequestBody FileTypeParam param){
        log.info("FileTypeController info saveOrUpdate FileTypeParam={}", param);
        Boolean result = fileTypeManager.saveOrUpdate(param);
        if (result) {
            return CommonResult.success(true);
        }
        if(Objects.isNull(param.getId())){
            return CommonResult.fail("新增失败");
        }
        return CommonResult.fail("更新类型");
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除")
    public CommonResult<Boolean> delete(@RequestBody FileTypeParam param){
        log.info("FileTypeController info delete FileTypeParam={}", param);
        Boolean result = fileTypeManager.delete(param);
        if (result) {
            return CommonResult.success(true);
        }
        return CommonResult.fail("删除失败");
    }

}

